'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 玩家信息
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var Code = require('../../../consts/code');
var roleInfoService = require('../../../services/roleInfoService');
var PlayerManager = require('../../../libs/playerManager');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 玩家信息
 */
Handler.prototype.getRoleInfo = function (msg, session, next) {
    var role_id = msg.role_id;
    if (!role_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var role_info;
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'bag', 'skill', 'vip', 'battle', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var role_model = player_manager.role;
                var bag_model = player_manager.bag;
                var vip_model = player_manager.vip;
                var battle_model = player_manager.battle;

                // 玩家信息
                role_info = roleInfoService.getRoleInfo(role_model, bag_model, vip_model, battle_model);
                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: role_info
            });
        }
    );
};
